const app = getApp()
var core = require('../../utils/core.js')

Page({
  data: {
    arr: [],
    navigation_flag:false,
    navigation_index_1:-1,
    navigation_index_2:-1,
    navigation_index_3:-1,
    navigation_index_4:-1,

    navigation:[],
    triggered: false,
    sider:[],
    data:[],     
     page:1,
     id:0
  },
  
  onLoad:function(options){
    if (options != null&& options!= undefined&&options.openid) {
      wx.setStorageSync('otheropenid', options.openid)  
    }
    if (options.id) {
      this.setData({id:options.id})  
    }
    this.getList();
    this.GetChoose()
  },
  onReady: function () {
    const arr = []
    for (let i = 0; i < 100; i++) arr.push(i)
    this.setData({
      arr
    })
   // this.getList()
    setTimeout(() => {
      this.setData({
        triggered: true,
      })
    }, 100)

   
    
  },

  onPulling(e) {
    console.log('onPulling:', e)
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    var self=this
    self.setData({fresh:true})
    // setTimeout(function(){
    //   self.setData({fresh:false})
    // },1000)
    self.setData({data:[],page:1})
    this.getList()
  },
  // onReachBottom(){
  //     this.getList()
  // },
  more:function(){
   // console.log(1222)
   // this.getList()
  },


  onRestore(e) {
    console.log('onRestore:', e)
  },

  onAbort(e) {
    console.log('onAbort', e)
  },
    getList:function(){
      
      //this.data=this.data1
      wx.showLoading({
        title: '加载中',
      })
      var self=this
      var data={
        page:self.data.page,
        op:'OnlineChain',
        industry_id:self.data.id
      }
      console.log(data);
      core.post("label",data,function(res){
        var data=self.data.data
        if(res.code==1){
           for (let i = 0; i < res.data.data.length; i++) {
               res.data.data[i]['outurl']=encodeURIComponent("/tp"+"?id="+res.data.data[i]['id']);
               data.push(res.data.data[i])             
           }
           self.setData({data:data,page:self.data.page+1})
        }
        wx.hideLoading();
        wx.stopPullDownRefresh()
      })
      
  },
  release:function(){
    wx.navigateTo({
      url: '/pages/release/release'　
    })
  },
  /**
   * 选择筛选类型
   */
  choose_type:function(res){
    var data=res.currentTarget.id.split('&&')
    var index=data[1]
    
    this.setData({
      [index]:data[0]
    })
    var level=data[1].split('index_')[1]*1
    
    for (let i =level+1; i <=4; i++) {
      index= 'navigation_index_'+i
      console.log(index)
      this.setData({
        [index]:-1
      }) 
    }
    console.log(this.data)
    //把本级true  下级变成false
    var self=this
    

    var data_lin=[];
    var data_str=''
    var data_arr=[];
    if(level*1==1){
      data_lin=self.data.navigation;
      for (let i = 0; i < data_lin.length; i++) {
        if(i==data[0]) {
            if(data_lin[i].flag==false){
              data_lin[i].flag=true
              this.setData({navigation_flag:1})
            }else{
              data_lin[i].flag=false
              this.setData({navigation_flag:0})
            }
        } else{
           data_lin[i].flag=false;
        }     
      }
      // data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      // data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      // data_lin[self.data.navigation_index_1].children=data_arr
      // data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      //self.setData({navigation:data_lin})

      self.setData({navigation:data_lin})
     
    }
    if(level*1==2){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children=data_arr
      data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      self.setData({navigation:data_lin})
    }
    if(level*1==3){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[data[0]].flag=true
      self.setData({navigation:data_lin})
      console.log(data_lin)
    }
    if(level*1==4){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[data[0]].flag=true
      self.setData({navigation:data_lin})
      console.log(data_lin)
    }
   
  },
  GetChoose(){
    var self=this
    core.post("Choose",{},function(res){
      console.log(res)
      if(res.code==1){
          self.setData({
            navigation:res.data,
          })
        
      }
    })
  },
  choose_con:function(target){
    var id=target.currentTarget.id;
    var self=this
    var data=self.data.navigation;
    if(id==0){
       data[self.data.navigation_index_1].flag=false;
       this.setData({navigation:data,navigation_flag:false})
    }
    if(id==1){
      
      var name=[];
      

      if(self.data.navigation_index_2!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].name
      }
      if(self.data.navigation_index_3!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].name
      }
      if(self.data.navigation_index_4!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4].name
      }
      data[self.data.navigation_index_1].flag=false;
      data[self.data.navigation_index_1].name=name.join(",");
      
      //console.log(name)
      //this.setData({navigation:data,navigation_flag:false})

      var data_str=JSON.stringify(data)
      var data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      
      self.setData({navigation:data_arr,navigation_flag:false,navigation_index_1:-1,navigation_index_2:-1,navigation_index_3:-1,navigation_index_4:-1})

   }
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.share()
  },
    /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    return this.share()
  },
  share:function(){
    return {
      title:  '园域--招商项目',
      path: '/pages/localproject/index?openid=' +wx.getStorageSync('openid1'),
      imageUrl:  ''//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    }
  }
  
})
